import api from '../../../api/index'
Page({
  data: {
    getdata: {},
    result: null,
    resultindex: 0,
    list: [
      { show: false, name: '膝盖', image: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/sportsClass/1.png', greyimage: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/sportsClass/3.png' },
      { show: false, name: '腰部', image: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/sportsClass/2.png', greyimage: 'http://lk-webfont.oss-cn-beijing.aliyuncs.com/redstart/sportsClass/4.png' },
    ]
  },
  onLoad(options) {
    wx.showLoading({
      title: '加载中...'
    })
    this.get()
    const { once } = options
    // if (options.once) {
    //   this.setData({
    //     once
    //   });
    // }
  },
  async get() {
    try {
      const res = await api.post('/api/user/storage/get', {
        data: {
          'key': 'hxhealthsporttags'
        }
      })
      console.log(res);
      let array = res[0].value
      const list = this.data.list;
      console.log(array);
      if (array) {
        array = JSON.parse(array)
        this.setData({
          getdata: array
        });
        console.log(array, '----------------------');
        if (array.injuryillness) {
          // 有选择
          console.log('222');
          if (array.injuryillness) {
            let injuryillnessList = array.injuryillness
            console.log(injuryillnessList);
            // injuryillnessList = '膝盖'
            injuryillnessList = injuryillnessList.split(',')
            console.log(injuryillnessList);
            list.forEach(item => {
              item.show = false
              injuryillnessList.forEach(tag => {
                console.log(tag);
                if (tag == item.name) {
                  item.show = true;
                }
              })
            })
            console.log(list);
            this.setData({
              list: list,
              loading: false
            });
          }
        } else {
          console.log('222221');
          let shape = this.data.list[0].name
          this.setData({
            result: shape
          });
        }
      }
      wx.hideLoading()
    } catch (e) {
      console.log('get->', e)
      return null
    }
  },
  goNext() {
    let list = this.data.list
    const filteredList = list.filter(item => item.show);
    const names = filteredList.map(item => item.name);
    const result = names.join(',');
    console.log("show为true的对象的name属性值：", result);
    this.setData({
      result
    });
    this.putData()
  },
  async putData() {
    let getdata = this.data.getdata
    console.log(getdata);
    console.log(this.data.result);
    getdata.injuryillness = this.data.result
    console.log(getdata);
    try {
      const res = await api.post('/api/user/storage/set', {
        data: {
          'key': 'hxhealthsporttags',
          'value': JSON.stringify(getdata)
        }
      })
      wx.showToast({
        title: '保存成功',
        icon: 'none',
        duration: 3000
      })
      wx.navigateTo({ url: '/sportsClass/pages/motiontime/index' });
      console.log(res);
      return res
    } catch (e) {
      console.log('put->', e)
      return null
    }
  },
  selectChange(e) { //滑动回调
    console.log(e);
    let index = e.currentTarget.dataset.index;
    let list = this.data.list
    list[index].show = !(this.data.list[index].show)
    console.log(list);
    this.setData({
      resultindex: index,
      list
    });
  }
})